﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace WowGuildSite.Armory
{
    public class ArmoryGuild : ArmoryBase, IEnumerable<ArmoryCharacter>
    {
        private class PageGrabber
        {
            private string              name;
            private int                 pageNumber;
            private IArmoryDataSource   dataSource;

            public string GetPageData()
            {
                return dataSource.GetGuildInfoPageData(name, pageNumber);
            }

            public PageGrabber( IArmoryDataSource dataSource,string name,int pageNumber)
            {
                this.pageNumber = pageNumber;
                this.dataSource = dataSource;
                this.name       = name;
            }
        }

        private string                      name;
        private List<ArmoryXmlDocument>     guildPages          = new List<ArmoryXmlDocument>();
        private List<PageGrabber>           guildPageGrabbers   = new List<PageGrabber>();
        

        public string                       Name
        {
            get { return name; }
        }

        public CharacterFaction             Faction
        {
            get
            {             
                foreach (ArmoryCharacter character in this)
                {
                    return character.Faction;
                }
                
                return CharacterFaction.Alliance;
            }
        }

        public ArmoryIcon                   FactionIcon
        {
            get
            {
                return Accessor.GetIcon(IconType.Faction, Faction);
            }
        }

        public string                       LocalizedFaction
        {
            get
            {
                foreach (ArmoryCharacter character in this)
                {
                    return character.LocalizedFaction;
                }

                return "Alliance";
            }
        }

        public int                          Count
        {
            get
            {
                if (guildPages.Count > 0)
                {
                    ArmoryXmlDocument firstPage = guildPages[0];
                    return firstPage.GetAttributeInt("/page/guildInfo/guild/members/@memberCount");
                }

                return 0;
            }
        }

        public IEnumerator<ArmoryCharacter> GetEnumerator()
        {
            foreach (ArmoryXmlDocument gp in guildPages)
            {
                IList<XmlElement> characterElements = gp.GetElements("/page/guildInfo/guild/members/character");

                foreach (XmlElement characterElement in characterElements)
                {
                    yield return Accessor.GetCharacter(characterElement.GetAttribute("name") );
                }
            }
        }

        internal int                        GetGuildRank(string characterName)
        {
            foreach (ArmoryXmlDocument gp in guildPages)
            {
                IList<XmlElement> characterElements = gp.GetElements("/page/guildInfo/guild/members/character");

                foreach (XmlElement characterElement in characterElements)
                {
                    string foundName = characterElement.GetAttribute("name");

                    if (foundName == characterName)
                    {
                        string rank = characterElement.GetAttribute("rank");
                        return 10 - int.Parse(rank);
                    }
                }
            }

            return 0;
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        internal ArmoryGuild( ArmoryAccessor accessor,
                            string name)
            : base(accessor)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException("name");
            }

            this.name     = name;
            int pageCount = Accessor.DataSource.GetGuildInfoPageCount(name);

            for (int i = 0; i < pageCount; i++)
            {
                PageGrabber grabber = new PageGrabber(Accessor.DataSource, name, i);
                guildPageGrabbers.Add(grabber);

                ArmoryXmlDocument pageDocument = new ArmoryXmlDocument( new ArmoryXmlDocument.ContentRetrievalDelegate( grabber.GetPageData ) );
                guildPages.Add(pageDocument);
            }
        }
    }
}
